package sctu.edu.java.year2016.team08.project;

/**
 * Created by 松红 on 2016/6/29.
 */

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class AScanf extends JFrame {
    public AScanf() {
        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        JTextField field3 = new JTextField();
        contentPane.add(field3, BorderLayout.NORTH);
        JTextField field = new JTextField();
        contentPane.add(field, BorderLayout.CENTER);
        JButton button = new JButton("点击提交");
        contentPane.add(button, BorderLayout.SOUTH);
        setVisible(true);
        setTitle("IT运维答案输入系统");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                } catch (ClassNotFoundException e2) {
                    e2.printStackTrace();
                }
                try {
                    String url = "jdbc:mysql://localhost:3306/lsongh";
                    String user = "root";
                    String Password = "";
                    Connection conn = DriverManager.getConnection(url, user, Password);
                    String string = null;
                    String string1 = null;
                    string = field.getText();
                    string1 = field3.getText();
                    String sql = "insert into itrun(question,answer) values('" + string1 + "','" + string + "')";
                    PreparedStatement ps = conn.prepareStatement(sql);
                    ps.execute(sql);
                    field.setText(" ");
                    field3.setText(" ");
                    conn.close();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
        });
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new AScanf();
    }

}

